home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.uwo.ca!usenet
- From: turnbull@canlon.physics.uwo.ca (David Turnbull)
- Newsgroups: comp.lang.c++
- Subject: Re: "free"ing classes when the "new" is inside a function
- Date: Fri, 15 Mar 1996 21:26:26 GMT
- Organization: University of Western Ontario
- Message-ID: <4icna3$7p3@falcon.ccs.uwo.ca>
- References: <4ic7a5$94u@falcon.ccs.uwo.ca> <3149B5B8.5C25@bnr.ca>
- NNTP-Posting-Host: ditto.physics.uwo.ca
- X-Newsreader: Forte Free Agent 1.0.82
-
- Joseph Bell <jobell@bnr.ca> wrote:
-
- >David Turnbull wrote:
- >>
- >> I have code something like
- >>
- >> MyClass* Myfunc(){
- >> Class* value;
- >> .
- >> .
- >> value = new MyClass(........); <- 1 of several possible constructors
- >> .
- >> return value;
- >> }
- >>
- >> ------ in main---------
- >> .
- >> Class* value;
- >> value=Myfunc();
- >> .
- >>
- >> How do I free up the memory allocated to value?
- >> delete value won't do it.
-
- >What is the difference between MyClass and Class?
-
- >Consider:
-
- >A* returnPtrtoA()
- >{
- > A* returnPtr = new A(...);
- > return returnPtr;
- >}
-
- >void main (void)
- >{
- > A* myAPtr = returnPtrtoA();
- >
-
- > delete myAPtr; // frees object and calls destructor
- >}
-
- >That works.
-
- Sorry, I got careless in my example. Class and MyClass were supposed
- to be the same. In other words, like your example. However, this does
- NOT free up the memory!
- Or perhaps my simplification of the problem is hiding something. What
- I have is closer to
-
-
- MyClass* Level1()
- {
- MyClass* result;
- result=Level2();
- return result;
- }
-
- MyClass* Level2()
- {
- MyClass* result;
- result=Level3();
- return result;
- }
-
- MyClass* Level3()
- {
- MyClass* result;
- result=new MyClass(..........);
- return result;
- }
-
- void main(void)
- {
-
- MyClass* value;
- value=Level1();
- delete value;
-
- }
-
- In this case the delete is not freeing memory.
- David Turnbull
- Department of Physics
- University of Western Ontario
- London, Ontario, Canada
-
-